/*
* Copyright 2016 YONYOU Corporation Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project         marketing

* @Package         com.yonyou.marketing.message.push.provider

* @Author          LuZhen

* @FileName        PushThread

* @Date            16/11/5

----------------------------------------------------------------------------------
*     Date          Who             Version         Comments
* 1. 16/11/5        LuZhen          1.0             Create Source
*
----------------------------------------------------------------------------------
*/
package com.yonyou.gmmc.service.wechatpush.provider;


import com.yonyou.gmmc.service.wechatpush.provider.impl.MessageProvideAdapterLocalImpl;
import com.yonyou.gmmc.service.wechatpush.resolver.PushMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.marketing.common.constants.MessagePushConstant;

import java.util.concurrent.Callable;

/**
 * 推送线程
 */
@Component(MessagePushConstant.PROVIDER_THREAD)
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class PushThreadCallable implements Callable<MessagePushResult>{

    public void setProviderMessage(PushMessage pushMessage) {
        this.pushMessage = pushMessage;
    }

    private PushMessage pushMessage;

    @Autowired
    @Qualifier("MessageProvideAdapterLocalImpl")
    private MessageProvideAdapter adapter;

    private final static Logger logger = Logger.getLogger(PushThreadCallable.class);

    /**
     * 线程执行逻辑
     * @return MessagePushResult
     */
    protected MessagePushResult execute() throws Exception{
        //转换消息
        ProviderMessage providerMessage = adapter.trans2Provider(pushMessage);
        //推送消息
        MessagePushResult result = adapter.push2Provider(providerMessage);
        logger.info("thread "+Thread.currentThread().getName()+" send result "+result);
        return result;
    }

    @Override
    public MessagePushResult call() throws Exception {
        logger.info("thread "+Thread.currentThread().getName()+" start ");
        MessagePushResult result= execute();
        logger.info("thread "+Thread.currentThread().getName()+" end ");
        return result;
    }


}
